// 实例化 请求拦截器 响应拦截器
// 封装axios
import axios from "axios";
import { getToken } from './token'
import { history } from "./history";
const http = axios.create({
    baseURL: 'http://geek.itheima.net/v1_0',
    timeout: 5000
})
// 添加请求拦截器
http.interceptors.request.use((config) => {
    const token = getToken()
    if (token) {
        config.headers.Authorization = `Bearer ${token}`
    }
    return config
}, (error) => {
    return Promise.reject(new Error('faile'))
})
// 响应拦截器
http.interceptors.response.use((response) => {
    return response.data
}, (error) => {
    console.dir(error)
    if (error.response.status === 401) {
        history.push('/login')
    }
    return Promise.reject(error)
})
export { http }